package com.miplus.ccrm.report.service.cti;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.common.JSysUser;
import com.miplus.ccrm.common.JUsrPerm;
import com.miplus.ccrm.report.controller.vo.cti.*;
import com.miplus.ccrm.report.controller.vo.rep.DistCaseNumReqVo;
import com.miplus.ccrm.report.controller.vo.rep.DistCaseNumResVo;
import com.miplus.ccrm.report.controller.vo.rep.RecyAgDayReqVo;
import com.miplus.ccrm.report.controller.vo.rep.RecyAgDayResVo;
import com.miplus.ccrm.report.pojo.CtiCallhist;
import com.miplus.ccrm.report.pojo.ReportCallHist;
import com.miplus.ccrm.report.pojo.TRecorder;
import com.miplus.ccrm.report.service.ExportService;
import com.miplus.ccrm.report.service.cti.ReportCallHistService;
import com.miplus.ccrm.report.mapper.ReportCallHistMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;

/**
* @author pc
* @description 针对表【report_call_hist】的数据库操作Service实现
* @createDate 2024-07-30 13:27:08
*/
@Service
public class ReportCallHistServiceImpl extends MPJBaseServiceImpl<ReportCallHistMapper, ReportCallHist>
    implements ReportCallHistService{

    @Resource
    private ExportService exportService;

    @Override
    public IPage<CallHistResvo> callHist(JSysUser user, CallHistReqVo paras)
    {
        //return baseMapper.callHist(new Page<>(paras.getPageNum(),paras.getPageSize()),usrid, paras);
        String $usrid=user.getUsrid();
        String phnstar= JUsrPerm.getPermission(user, JUsrPerm.DISSTAR_PHN);
        //2025-05-13
        boolean viewAllCase=JUsrPerm.viewAllFiCase(user);
        boolean viewGrpFiCase = false;
        if(!viewAllCase && user.getMgrgrplist().size()>0)
            viewGrpFiCase = true;
        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                CallHistResvo.class,
                new MPJLambdaWrapper<ReportCallHist>()
                        .selectAs(String.format("fi_starSecu('%s','%s',t.ani,'%s')",$usrid,JUsrPerm.DISSTAR_PHN,phnstar),CallHistResvo::getEncryptani)
                        .selectAs(String.format("fi_starSecu('%s','%s',t.dnis,'%s')",$usrid,JUsrPerm.DISSTAR_PHN,phnstar),CallHistResvo::getEncryptdnis)
                        .selectAs("fun_getgrpname(t.opeid)",CallHistResvo::getGrpname)
                        .selectAs("su.name",CallHistResvo::getUsrname)
                        .selectAs("fun_getnames('CTI.CALLRESULT',t.callresult)",CallHistResvo::getCallresultdesc)
                        .selectAs("case when t.talkspan>0 then 1 else 0 end",CallHistResvo::getIsconnect)
                        .selectAll(ReportCallHist.class)
                        .innerJoin("sys_user su on su.usrid = t.opeid")
                        .in(viewGrpFiCase,"su.groups",user.getMgrgrplist())
                        .eq(!viewAllCase && !viewGrpFiCase,ReportCallHist::getOpeid,$usrid)
                        .between(JPubUtils.IsNotEmpty(paras.getStartdt()), ReportCallHist::getStartdatetime,JPubUtils.StringToDate(paras.getStartdt()),JPubUtils.StringToDate(paras.getEnddt()))
                        .eq(JPubUtils.IsNotEmpty(paras.getCalltype()),ReportCallHist::getCalltype,paras.getCalltype())
                        .eq(JPubUtils.IsNotEmpty(paras.getCallresult()),ReportCallHist::getCallresult,paras.getCallresult())
                        .ge(JPubUtils.IsNotEmpty(paras.getTalkspan1()),ReportCallHist::getTalkspan,paras.getTalkspan1())
                        .le(JPubUtils.IsNotEmpty(paras.getTalkspan2()),ReportCallHist::getTalkspan,paras.getTalkspan2())
                        .apply(JPubUtils.IsNotEmpty(paras.getGrpid()),"fc_hasUsrInGroup(t.usrid,{0})>0",paras.getGrpid())
                        .eq(JPubUtils.IsNotEmpty(paras.getDnis()),ReportCallHist::getDnis,paras.getDnis())
                        .eq(JPubUtils.IsNotEmpty(paras.getAni()),ReportCallHist::getAni,paras.getAni())
                        .eq(JPubUtils.IsNotEmpty(paras.getDn()),ReportCallHist::getOpedevnum,paras.getDn())
                        .apply(JPubUtils.IsNotEmpty(paras.getPhone())," (t.ani ={0} or t.dnis={0})", paras.getPhone())
                        .eq(JPubUtils.IsNotEmpty(paras.getOpeid()),ReportCallHist::getOpeid, paras.getOpeid())
                        .eq(ReportCallHist::getDevtype,"device")
                        .orderByAsc(ReportCallHist::getStartdatetime)
        );
    }


    @Override
    public IPage<CallAgDayResVo> repAgentCallDay(String usrid, CallAgDayReqVo paras)
    {
        return baseMapper.repAgentCallDay(new Page<>(paras.getPageNum(),paras.getPageSize()),usrid,paras);
    }


    @Override
    public IPage<RecyAgDayResVo> recoveryTotal(String usrid, RecyAgDayReqVo paras)
    {
        return baseMapper.recoveryTotal(new Page<>(paras.getPageNum(),paras.getPageSize()),usrid,paras);
    }

    @Override
    public IPage<DistCaseNumResVo> assignTotal(String usrid, DistCaseNumReqVo paras)
    {
        return baseMapper.assignTotal(new Page<>(paras.getPageNum(),paras.getPageSize()),usrid,paras);
    }

    @Override
    public CmdResVo expAgentCallDay(String usrid, CallAgDayReqVo paras, HttpServletResponse response)
    {
        IPage<CallAgDayResVo> pages=repAgentCallDay(usrid, paras);
        ArrayList arr=new ArrayList();

        String exptempid=JPubUtils.getExpTempID(paras.getExpfilename(),"坐席呼叫统计日报");
        String filename=JPubUtils.getExpFileName(
                paras.getExpfilename(),exptempid
        );
        pages.convert(u->{
            HashMap map=JPubUtils.toExportMap(u);
            if(map!=null) arr.add(map);
            return u;
        });
        CmdResVo resVo=exportService.ToExcel(
                response,
                arr,
                paras.getExpidlist(),
                paras.getExpnamelist(),
                filename,
                usrid
        );

        return resVo;
    }

    @Override
    public CmdResVo expRecoveryTotal(String usrid, RecyAgDayReqVo paras, HttpServletResponse response)
    {
        IPage<RecyAgDayResVo> pages=recoveryTotal(usrid, paras);
        ArrayList arr=new ArrayList();

        String exptempid=JPubUtils.getExpTempID(paras.getExpfilename(),"回收表现统计表");
        String filename=JPubUtils.getExpFileName(
                paras.getExpfilename(),exptempid
        );
        pages.convert(u->{
            HashMap map=JPubUtils.toExportMap(u);
            if(map!=null) arr.add(map);
            return u;
        });
        CmdResVo resVo=exportService.ToExcel(
                response,
                arr,
                paras.getExpidlist(),
                paras.getExpnamelist(),
                filename,
                usrid
        );

        return resVo;
    }

    @Override
    public CmdResVo expAssignTotal(String usrid, DistCaseNumReqVo paras, HttpServletResponse response)
    {
        IPage<DistCaseNumResVo> pages=assignTotal(usrid, paras);
        ArrayList arr=new ArrayList();

        String exptempid=JPubUtils.getExpTempID(paras.getExpfilename(),"分配案件数量统计表");
        String filename=JPubUtils.getExpFileName(
                paras.getExpfilename(),exptempid
        );
        pages.convert(u->{
            HashMap map=JPubUtils.toExportMap(u);
            if(map!=null) arr.add(map);
            return u;
        });
        CmdResVo resVo=exportService.ToExcel(
                response,
                arr,
                paras.getExpidlist(),
                paras.getExpnamelist(),
                filename,
                usrid
        );

        return resVo;
    }
}




